package net.tatu.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.google.code.kaptcha.Constants;
import net.tatu.base.BaseController;
import net.tatu.base.Json;
import net.tatu.base.Page;
import net.tatu.model.Location;
import net.tatu.model.Model;
import net.tatu.model.ModelLevel;
import net.tatu.model.OrgBusinessTargetRela;
import net.tatu.model.ProjInvite;
import net.tatu.model.ProjInviteSplit;
import net.tatu.model.PubCode;
import net.tatu.model.SysLog;
import net.tatu.model.Target;
import net.tatu.model.UploadFile;
import net.tatu.model.User;
import net.tatu.model.UserBankCard;
import net.tatu.model.UserPremAccount;
import net.tatu.model.UserPremDetail;
import net.tatu.service.ModelService;
import net.tatu.service.OrgBusinessTargetRelaService;
import net.tatu.service.ProjInviteService;
import net.tatu.service.PubCodeService;
import net.tatu.service.SendNoticeService;
import net.tatu.service.SysLogService;
import net.tatu.service.LocationService;
import net.tatu.service.TargetService;
import net.tatu.service.UploadService;
import net.tatu.service.UserBankCardService;
import net.tatu.service.UserPointService;
import net.tatu.service.UserPremAccountService;
import net.tatu.service.UserService;
import net.tatu.util.Util;

@Controller
@RequestMapping("/bid")
public class BidController extends BaseController {
	
	private static Logger log = Logger.getLogger("UserController.class");
	@Autowired
	private UserService userService;
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private LocationService locationService;
	@Autowired
	private PubCodeService pubCodeService;
	@Autowired
	private UploadService uploadService;
	@Autowired
	private SendNoticeService sendNoticeService;
	@Autowired
	private OrgBusinessTargetRelaService orgBusinessTargetRelaService;
	@Autowired
	private TargetService targetService;
	@Autowired
	private ModelService modelService;
	@Autowired
	private ProjInviteService projInviteService;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@RequestMapping("/toBid")
	public ModelAndView toBid(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/bid/bid");
		User user = (User) request.getSession().getAttribute("user");
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping("/toTender")
	public ModelAndView toTender() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/bid/tender");
		List<Target> targets = targetService.getTargets();
		List<Location> locations = locationService.getLocations();
		List<PubCode> depthCodes = pubCodeService.getDepthCodes();
		List<PubCode> qualities = pubCodeService.getComQualityCodes();
		mav.addObject("depthCodes", depthCodes);
		mav.addObject("qualities", qualities);
		mav.addObject("targets", targets);
		mav.addObject("locations", locations);
		log.debug("this is tomain method!");
		return mav;
	}
	
	@RequestMapping("/wtTender")
	public void wtTender(ProjInvite projInvite, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		User u = (User) request.getSession().getAttribute("user");
		
		String ret = projInviteService.wtTender(projInvite, u);
		if(ret.equals("true")){
			String url = (String) request.getSession().getAttribute("tenderFileUrl");
			String fileName = (String) request.getSession().getAttribute("tenderFileName");
			if(null != url){//用户存在上传的文件保存新的上传文件
				String ret2 = projInviteService.addTenderFile(projInvite, u, url, fileName);
				if(ret2.equals("true")){
					request.getSession().removeAttribute("tenderFileUrl");
					request.getSession().removeAttribute("tenderFileName");
				}
			}
			j.setSuccess(true);
			j.setMsg("委托分标成功");
			//记录日志
//			sysLogService.newLog(9803, 9701, 9901, u.getUserId());
		} else {
			j.setMsg("委托分标失败");
		}
		super.writeJson(j, response);
	}
	
	@RequestMapping("/zzTender")
	public void zzTender(ProjInviteSplit projInviteSplit, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		User u = (User) request.getSession().getAttribute("user");
		String picUrl = (String) request.getSession().getAttribute("tenderPicUrl");
		projInviteSplit.setInvitePicUrl(picUrl);
		String ret = projInviteService.zzTender(projInviteSplit, u);
		if(ret.equals("true")){
			String url = (String) request.getSession().getAttribute("tenderFileUrl");
			String fileName = (String) request.getSession().getAttribute("tenderFileName");
			if(null != url){//用户存在上传的文件保存新的上传文件
				String ret2 = projInviteService.addTenderFile(projInviteSplit, u, url, fileName);
				if(ret2.equals("true")){
					request.getSession().removeAttribute("tenderFileUrl");
					request.getSession().removeAttribute("tenderFileName");
				}
			}
			j.setSuccess(true);
			j.setMsg("自主分标成功");
			//记录日志
//			sysLogService.newLog(9803, 9701, 9901, u.getUserId());
		} else {
			j.setMsg("自主分标失败");
		}
		super.writeJson(j, response);
	}
	
	@RequestMapping("uploadFile")
	@ResponseBody
	public Object uploadFile(HttpServletRequest request, UploadFile file){
		//imagType=(4:委托分标文件；0:个人分标)
		String imgType = request.getParameter("imgType");
		String type = request.getParameter("type");
		String path = request.getSession().getServletContext().getContextPath();
		boolean flag = false;
		String errorMessage = "";  
		try{
			String url = uploadService.uploadForm(file, imgType, request);
			if(url != null){
				flag = true;
				url = (String) url.subSequence(url.indexOf(request.getContextPath()) + request.getContextPath().length(), url.length());
				User user = (User) request.getSession().getAttribute("user");
				if(imgType.equals("1")){
					user.setUserCertFrontPhotoUrl(url);
				}else if(imgType.equals("0")){
					user.setUserCertObversePhotoUrl(url);
				}else if(imgType.equals("2")){
					user.setOrgBusinessCertPhotoUrl(url);
				}else if(imgType.equals("3")){
					user.setOrgTaxRegCertPhotoUrl(url);
				}else if(imgType.equals("4")){//委托分标文件，先生成一个文件对象放置在缓存，等待用户提交分标对象后在进行存储
					request.getSession().setAttribute("tenderFileUrl", url);
					request.getSession().setAttribute("tenderFileName", file.getImgFile().getOriginalFilename());
				}else if(imgType.equals("5")){//委托分标文件，先生成一个文件对象放置在缓存，等待用户提交分标对象后在进行存储
					request.getSession().setAttribute("tenderPicUrl", url);
					request.getSession().setAttribute("tenderPicName", file.getImgFile().getOriginalFilename());
				}else{
					log.error("imgType 不合法");
				}
				errorMessage = url + "&" + imgType;
				boolean b = userService.updateUser(user);
				request.getSession().setAttribute("imgType" + imgType, 1);
			}
		}catch (Exception e){
			log.error("firstUpload failed!" , e);
			errorMessage = "文件上传失败!";
		}
		if(flag){
			if(null != type && Integer.valueOf(type) == 2){
				//上传成功，返回到前台，调用uploadSucced()这个方法
	            return "<script>window.parent.uploadSucced2('" + errorMessage + "');</script>";
			}else{
				//上传成功，返回到前台，调用uploadSucced()这个方法
	            return "<script>window.parent.uploadSucced('" + errorMessage + "');</script>";
			}
	        
	}
		if(null != type && Integer.valueOf(type) == 2){
			//上传失败，返回到前台，调用uploadFailed()这个方法
			return "<script>window.parent.uploadFailed2('" + errorMessage + "');</script>";
		}else{
			//上传失败，返回到前台，调用uploadFailed()这个方法
			return "<script>window.parent.uploadFailed('" + errorMessage + "');</script>";
		}
	
	}
	
	@RequestMapping("/tenderOK")
	public ModelAndView tenderOK() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/bid/tender_ok");

		return mav;
	}
	
	@RequestMapping("/toArea")
	public ModelAndView toArea() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/bid/area");
		log.debug("this is toLogin method!");
		return mav;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping("/toRegister")
	public ModelAndView toRegister() {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/register");
		log.debug("this is toRegister method!");
		return mav;
	}
	
	@RequestMapping("/login")
	public void login(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		//处理用户名是电话还是邮箱的逻辑
		String name = user.getUserName();
		if (Util.isEmail(name)){
			user.setUserEmail(name);
			user.setUserName(null);
		}else if(Util.isMobile(name)){
			user.setUserMobile(name);
			user.setUserName(null);
		}else{

		}
		
		User u = userService.login(user);
		if(u!=null){
			j.setSuccess(true);
			j.setMsg("登录成功");
			request.getSession().setAttribute("user", u);
			//记录日志
			sysLogService.newLog(9803, 9701, 9901, u.getUserId());
		} else {
			j.setMsg("用户名或密码错误");
		}
		log.debug("message about json:" + j.toString());
		
		super.writeJson(j, response);
	}
	
	@RequestMapping("/register")
	public void register(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		String name = user.getUserName();
		if (Util.isEmail(name)){
			user.setUserEmail(name);
			user.setUserName(null);
		}else if(Util.isMobile(name)){
			user.setUserMobile(name);
			user.setUserName(null);
		}else{

		}
		if((Boolean) request.getSession().getAttribute("isVerifyMobileCode")){
			if((Boolean) request.getSession().getAttribute("isVerifyCode")){
				User u = userService.get(user);
				if (u == null){
					user.setUserName(name);
					u = userService.register(user);
					if(u!=null){
						j.setSuccess(true);
						j.setMsg("注册成功");
						request.getSession().setAttribute("user", u);
						//记录日志
						sysLogService.newLog(9801, 9701, 9901, u.getUserId());
					} else {
						j.setMsg("请正确填写数据");
					}
				}else{
					j.setMsg("用户已存在！");
				}
			}else{
				j.setMsg("验证码错误！");
			}
		}else{
			j.setMsg("手机验证码错误！");
		}
		super.writeJson(j, response);
	}
	
	@RequestMapping("/verifyUser")
	public void verifyUser(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		User u = userService.get(user);
		try {
			if(u != null){
				j.setSuccess(true);
				j.setMsg("1");// 用户名存在
			}else {
				j.setMsg("2");// 用户名不存在
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		super.writeJson(j, response);
	}
	
	@RequestMapping("/getVerifyMobileCode")  
    public void getVerifyMobileCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
        try {
        	String code = Util.getVerifyMobileCode();
        	boolean flag = true;//sendVerifyMobileCode();//发送手机验证码给用户
			if(flag){
	            request.getSession().setAttribute("verifyMobileCode", code);
				j.setSuccess(true);
				j.setMsg("手机验证码发送成功！" + code);// 手机验证码发送成功
			}else {
				j.setMsg("手机验证码发送失败！");// 手机验证码发送失败
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		super.writeJson(j, response);
    }  
	
	@RequestMapping("/verifyMobileCode")  
    public void verifyMobileCode(String verifyMobileCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
        String code = (String) request.getSession().getAttribute("verifyMobileCode");//获取生成的手机验证码   
        System.out.println("mobile:" + verifyMobileCode + " code:" + code);
        try {
			if(verifyMobileCode.equals(code)){
				request.getSession().setAttribute("isVerifyMobileCode", true);
				j.setSuccess(true);
				j.setMsg("1");// 验证通过
			}else {
				request.getSession().setAttribute("isVerifyMobileCode", false);
				j.setMsg("2");// 验证不通过
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		super.writeJson(j, response);
    }  
	
	@RequestMapping("/verifyCode")  
    public void verifyCode(String verifyCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
        String code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);//获取生成的验证码   
        try {
			if(verifyCode.equals(code)){
				request.getSession().setAttribute("isVerifyCode", true);
				j.setSuccess(true);
				j.setMsg("1");// 验证通过
			}else {
				request.getSession().setAttribute("isVerifyCode", false);
				j.setMsg("2");// 验证不通过
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		super.writeJson(j, response);
    }  
	
	@RequestMapping("/toRegisterSuccess")
	public ModelAndView toRegisterSuccess(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/register_success");
		return mav;
	}
	
	@RequestMapping("/toCertify")
	public ModelAndView toCertify(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/certify");
		return mav;
	}
	
	@RequestMapping("/toDemindCertify")
	public ModelAndView toDemindCertify(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/demindCertify");
		return mav;
	}
	
	@RequestMapping("/toPerDemindCertify")
	public ModelAndView toPerDemindCertify(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/perDemindCertify");
		//添加页面信息
		User user = (User) request.getSession().getAttribute("user");
		List<Location> locations = locationService.getLocations();
		List<PubCode> certs = pubCodeService.getCertTypes();
		log.debug("locations lenth:" + locations.size() + " certs:" + certs.size());
		mav.addObject("certs", certs);
		mav.addObject("locations", locations);
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping("/perDemindCertify")
	public void perDemindCertify(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		log.debug("get user:" + user.toString());
//		String img1 = String.valueOf( request.getSession().getAttribute("imgType1"));
//		String img0 = String.valueOf( request.getSession().getAttribute("imgType0"));
//		String img2 = String.valueOf( request.getSession().getAttribute("imgType2"));
//		log.debug("img1:" + img1 + "img2:" + img2 + "img0:" + img0);
//		if(null != img1 && null != img0 && img1.equals("1") && img0.equals("1")){
			String verify = userService.verifyUser(user);
			User u = (User) request.getSession().getAttribute("user");
			u.setUserName(user.getUserName());
			u.setUserCertTypeCode(user.getUserCertTypeCode());
			u.setUserCertNo(user.getUserCertNo());
			u.setUserMobile(user.getUserMobile());
			u.setUserEmail(user.getUserEmail());
			u.setUserAddrDetail(user.getUserAddrDetail());
			u.setUserLocationCode(user.getUserLocationCode());
			u.setUserCurrPosition(user.getUserCurrPosition());
			u.setUserWorkYears(user.getUserWorkYears());
			u.setBackupContactPersonName(user.getBackupContactPersonName());
			u.setBackupContactPersonMobile(user.getBackupContactPersonMobile());
			u.setUserCertFrontPhotoUrl(user.getUserCertFrontPhotoUrl());
			u.setUserCertObversePhotoUrl(user.getUserCertObversePhotoUrl());
			u.setUserTypeCode(10101);
			u.setUserKindCode(10301);
			
			u.setUserStatusCode(10503);
			log.debug("user: input the database:" + u.toString());
			userService.updateUser(u);
			j.setSuccess(true);
			log.debug("return json j:" + j.toString());
			//资料提交成功，通知用户
			sendNoticeService.createNotice(107001, u.getUserId());
//		}else{
//			j.setMsg("上传失败！请先上传证件照！");
//		}
		
		super.writeJson(j, response);
	}
	
	
	
	
	
	
	
	@RequestMapping("/toPerDemindCertifyResult")
	public ModelAndView toPerDemindCertifyResult(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/perDemindCertifyResult");
		return mav;
	}
	
	@RequestMapping("/toComDemindCertify")
	public ModelAndView toComDemindCertify(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/comDemindCertify");
		//添加页面信息
		User user = (User) request.getSession().getAttribute("user");
		List<Location> locations = locationService.getLocations();
		List<PubCode> certs = pubCodeService.getCertTypes();
		List<Target> targets = targetService.getTargets();
		//使用targets的recordstatus字段来临时记录用户是否曾经选择过这个选项。
		OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
		orgBusinessTargetRela.setUserId(user.getUserId());
		
		List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
		if(bTargets != null){
			log.debug("bTarget.size:" + bTargets.size());
			HashMap<Long, String> map = new HashMap<Long, String>();
			for(int i = 0; i < bTargets.size(); i++){
				OrgBusinessTargetRela o = bTargets.get(i);
				map.put(o.getOrgBusinessTargetCode(), null);
			}
			log.debug("map:" + map.toString());
			for(int j = 0; j < targets.size(); j++){
				Target t = targets.get(j);
				if(map.containsKey(t.getTargetCode())){
					t.setRecordStatusCode(2);
				}
			}
		}
		log.debug("locations lenth:" + locations.size() + " certs:" + certs.size());
		mav.addObject("certs", certs);
		mav.addObject("targets", targets);
		mav.addObject("locations", locations);
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping("/comDemindCertify")
	public void comDemindCertify(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		changeBusinessTargets(request);
		log.debug("get com user:" + user.toString());
		String verify = userService.verifyUser(user);
		User u = (User) request.getSession().getAttribute("user");
		u.setUserName(user.getUserName());
		u.setUserCertTypeCode(user.getUserCertTypeCode());
		u.setUserCertNo(user.getUserCertNo());
		u.setUserMobile(user.getUserMobile());
		u.setUserEmail(user.getUserEmail());
		u.setOrgRegAddr(user.getOrgRegAddr());
		u.setOrgRegLocationCode(user.getOrgRegLocationCode());
		u.setUserCurrPosition(user.getUserCurrPosition());
		u.setOrgTaxRegCertNo(user.getOrgTaxRegCertNo());
		u.setOrgName(user.getOrgName());
		u.setOrgLawfulManName(user.getOrgLawfulManName());
		u.setOrgCode(user.getOrgCode());
		u.setUserDesc(user.getUserDesc());
		u.setRemark(user.getRemark());
		u.setUserTypeCode(10102);
		u.setUserKindCode(10301);
		
		u.setUserCertFrontPhotoUrl(user.getUserCertFrontPhotoUrl());
		u.setUserCertObversePhotoUrl(user.getUserCertObversePhotoUrl());
		u.setOrgBusinessCertPhotoUrl(user.getOrgBusinessCertPhotoUrl());
		u.setOrgTaxRegCertPhotoUrl(user.getOrgBusinessCertPhotoUrl());
		
		
		u.setUserStatusCode(10503);
		log.debug("user: input the database:" + u.toString());
		userService.updateUser(u);
		j.setSuccess(true);
		log.debug("return json j:" + j.toString());
		//资料提交成功，通知用户
		sendNoticeService.createNotice(107001, u.getUserId());
		
		super.writeJson(j, response);
	}
	
	@RequestMapping("/toComDemindCertifyResult")
	public ModelAndView toComDemindCertifyResult(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/comDemindCertifyResult");
		return mav;
	}
	
	@RequestMapping("/toSupplyCertify")
	public ModelAndView toSupplyCertify(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/supplyCertify");
		//添加页面信息
		User user = (User) request.getSession().getAttribute("user");
		List<Location> locations = locationService.getLocations();
		List<PubCode> certs = pubCodeService.getCertTypes();
		List<PubCode> years = pubCodeService.getOrgBusinessYearsTypes();
		List<Target> targets = targetService.getTargets();
		//使用targets的recordstatus字段来临时记录用户是否曾经选择过这个选项。
		OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
		orgBusinessTargetRela.setUserId(user.getUserId());
		
		List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
		if(bTargets != null){
			log.debug("bTarget.size:" + bTargets.size());
			HashMap<Long, String> map = new HashMap<Long, String>();
			for(int i = 0; i < bTargets.size(); i++){
				OrgBusinessTargetRela o = bTargets.get(i);
				map.put(o.getOrgBusinessTargetCode(), null);
			}
			log.debug("map:" + map.toString());
			for(int j = 0; j < targets.size(); j++){
				Target t = targets.get(j);
				if(map.containsKey(t.getTargetCode())){
					t.setRecordStatusCode(2);
				}
			}
		}
		
		log.debug("locations lenth:" + locations.size() + " certs:" + certs.size() + " targets:" + targets.size());
		mav.addObject("certs", certs);
		mav.addObject("years", years);
		mav.addObject("targets", targets);
		mav.addObject("locations", locations);
		mav.addObject("user", user);
		return mav;
	}
	
	@RequestMapping("/supplyCertify")
	public void supplyCertify(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		//处理复选框经营范围信息
		changeBusinessTargets(request);
		log.debug("get supply user:" + user.toString());
		String verify = userService.verifyUser(user);
		User u = (User) request.getSession().getAttribute("user");
		u.setUserName(user.getUserName());
		u.setUserCertTypeCode(user.getUserCertTypeCode());
		u.setUserCertNo(user.getUserCertNo());
		u.setUserMobile(user.getUserMobile());
		u.setUserEmail(user.getUserEmail());
		u.setOrgRegAddr(user.getOrgRegAddr());
		u.setOrgRegLocationCode(user.getOrgRegLocationCode());
		u.setOrgCurrLocationCode(user.getOrgCurrLocationCode());
		u.setOrgCurAddr(user.getOrgCurAddr());
		u.setOrgTaxRegCertNo(user.getOrgTaxRegCertNo());
		u.setOrgName(user.getOrgName());
		u.setOrgLawfulManName(user.getOrgLawfulManName());
		u.setOrgCode(user.getOrgCode());
		u.setRemark(user.getRemark());
		u.setOrgRegCapital(user.getOrgRegCapital());
		u.setOrgPhoneNo(user.getOrgPhoneNo());
		u.setOrgBusinessYearsCode(user.getOrgBusinessYearsCode());
		u.setUserTypeCode(10102);
		u.setUserKindCode(10302);

		

		u.setOrgBusinessCertPhotoUrl(user.getOrgBusinessCertPhotoUrl());
		
		
		u.setUserStatusCode(10503);
		log.debug("user: input the database:" + u.toString());
		userService.updateUser(u);
		j.setSuccess(true);
		//资料提交成功，通知用户
		sendNoticeService.createNotice(107002, u.getUserId());
		
		super.writeJson(j, response);
	}
	
	private String changeBusinessTargets(HttpServletRequest request) {
		String targets = request.getParameter("targets");
		User u = (User) request.getSession().getAttribute("user");
		String[] tar = targets.split(",");
		String msg = "";
		
		OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
		orgBusinessTargetRela.setUserId(u.getUserId());
		HashMap<Long, String> map = new HashMap<Long, String>();
		List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
		if(bTargets != null){
			log.debug("bTarget.size:" + bTargets.size());
			
			for(int i = 0; i < bTargets.size(); i++){
				OrgBusinessTargetRela o = bTargets.get(i);
				o.setRecordStatusCode(2);
				int g = orgBusinessTargetRelaService.updateOrgBusinessTarget(o);
				map.put(o.getOrgBusinessTargetCode(), null);
			}
			
		}
		
		for(int i = 0; i < tar.length; i++){
			long t = Long.valueOf(tar[i]);
			if(map.containsKey(t)){
				int g = orgBusinessTargetRelaService.updateOrgBusinessTarget(new OrgBusinessTargetRela(t, u.getUserId()));
				if(g != 1){
					msg = msg + "更新数据出错userID:" + u.getUserId() + " targetCode:" + t;
				}
			}else{
				int g = orgBusinessTargetRelaService.insertOrgBusinessTarget(new OrgBusinessTargetRela(t, u.getUserId()));
				if(g != 1){
					msg = msg + "生成新数据出错userID:" + u.getUserId() + " targetCode:" + t;
				}
			}
			
		}
		return msg;
	}

	@RequestMapping("/toSupplyCertifyResult")
	public ModelAndView toSupplyCertifyResult(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/supplyCertifyResult");
		return mav;
	}
	
	@Autowired
	private UserPremAccountService userPremAccountService;
	
	@RequestMapping("/toIndividual")
	public ModelAndView toIndividual(HttpServletRequest request) {
		User u = (User) request.getSession().getAttribute("user");
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/individual");
		if(u.getUserStatusCode() != 10502){
			if(u.getUserStatusCode() == 10503){
				return new ModelAndView("/WEB-INF/pages/certifyWait");
			}else if(u.getUserStatusCode() == 10504){
				return new ModelAndView("/WEB-INF/pages/certifyFailed");
			}
			return new ModelAndView("redirect:/user/toCertify.do");
		}
		//检测是否创建过资金账户，若无则创建新的资金账户
		if(!userPremAccountService.checkUserPremAccount(u)){
			userPremAccountService.createUserPremAccount(u);
		}
		userPremAccountService.updateUserLevel(u);
		return mav;
	}
	
	@RequestMapping("/toAccountManage")
	public ModelAndView toAccountManage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountManage");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toAccountView")
	public ModelAndView toAccountView(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountView");
		User u = (User) request.getSession().getAttribute("user");
		UserPremAccount userPremAccount = new UserPremAccount();
		userPremAccount.setUserPremActId(u.getUserId());
		UserPremAccount upa = userPremAccountService.getUserPremAccount(userPremAccount);
		mav.addObject("user",u);
		mav.addObject("prem", upa);
		return mav;
	}
	
	@RequestMapping("/toAccountPremChange")
	public ModelAndView toAccountPremChange(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountPremChange");
		User u = (User) request.getSession().getAttribute("user");
		UserPremDetail userPremDetail = new UserPremDetail();
		userPremDetail.setUserPremActId(u.getUserId());
		List<UserPremDetail> upds = userPremAccountService.getUserPremDetail(userPremDetail);
		//获取所有的类型
		Map<Integer, String> maps = new HashMap<Integer, String>();
		for(int i = 0; i < upds.size(); i++){
			UserPremDetail upd = upds.get(i);
			maps.put(upd.getChangeTypeCode(), "");
		}
		List<Integer> types = new ArrayList();
		Collection c = maps.keySet();
		Iterator it = c.iterator();
		for(; it.hasNext();){
			types.add((Integer) it.next());
		}
		mav.addObject("types", types);
		mav.addObject("user",u);
		mav.addObject("userPremDetails", upds);
		return mav;
	}
	
	@RequestMapping("/searchPremChange")
	public ModelAndView searchPremChange(HttpServletRequest request) {
		String type = request.getParameter("type");
		String dateTime1 = request.getParameter("dateTime1");
		String dateTime2 = request.getParameter("dateTime2");
		System.out.println("type:" + type + " dateTime1:" + dateTime1 + " dateTime2:" + dateTime2);
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountPremChange");
		User u = (User) request.getSession().getAttribute("user");
		UserPremDetail userPremDetail = new UserPremDetail();
		userPremDetail.setUserPremActId(u.getUserId());
		List<UserPremDetail> upds = userPremAccountService.getUserPremDetail(userPremDetail);
		//获取所有的类型
		Map<Integer, String> maps = new HashMap<Integer, String>();
		for(int i = 0; i < upds.size(); i++){
			UserPremDetail upd = upds.get(i);
			maps.put(upd.getChangeTypeCode(), "");
		}
		List<Integer> types = new ArrayList();
		Collection c = maps.keySet();
		Iterator it = c.iterator();
		for(; it.hasNext();){
			types.add((Integer) it.next());
		}
		
		List<UserPremDetail> ret = new ArrayList<UserPremDetail>();
		for(int i = 0; i < upds.size(); i++){
			UserPremDetail upd = upds.get(i);
			if(upd.getChangeTypeCode() == Integer.valueOf(type)){
				if(Util.compareTime(upd.getCreateDate(), dateTime1, dateTime2)){
					ret.add(upd);
				}
			}
		}
		mav.addObject("types", types);
		mav.addObject("user",u);
		mav.addObject("userPremDetails", ret);
		return mav;
	}
	
	

	@RequestMapping("/toAccountRecharge")
	public ModelAndView toAccountRecharge(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountRecharge");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/recharge")
	public ModelAndView recharge(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountRechargeSuccess");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/towithdraw")
	public ModelAndView towithdraw(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountWithdraw");
		User u = (User) request.getSession().getAttribute("user");
		List<UserBankCard> cards = userBankCardService.getCards(u);
		if(cards == null){
			return new ModelAndView("redirect:/user/toAccountBankCardAdd.do");
		}
		mav.addObject("cards", cards);
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/withdraw")
	public ModelAndView withdraw(HttpServletRequest request) {
		String bankCardNo = request.getParameter("card");
		String num = request.getParameter("num");
		log.debug("得到提现的数据：num:" + num + " card:" + bankCardNo);
	
		
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountWithdrawSuccess");
		User u = (User) request.getSession().getAttribute("user");
		//提现的具体操作。
		boolean bo = userPremAccountService.withdraw(u, num, bankCardNo);
		log.debug("用户提现操作执行结果：" + bo);
		mav.addObject("user",u);
		return mav;
	}
	
	@Autowired
	private UserBankCardService userBankCardService;
	
	@RequestMapping("/toAccountBankCard")
	public ModelAndView toAccountBankCard(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountBankCard");
		User u = (User) request.getSession().getAttribute("user");
		//得到当前用户的所有银行卡
		List<UserBankCard> cards = userBankCardService.getCards(u);
		mav.addObject("cards", cards);
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toAccountBankCardAdd")
	public ModelAndView toAccountBankCardAdd(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountBankCardAdd");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/accountBankCardAdd")
	public void accountBankCardAdd(UserBankCard userBankCard, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		log.debug("得到的userBankCard：from form表单：" + userBankCard.toString());
		boolean bo = userBankCardService.addBankCard(request, userBankCard);
		if(bo){
			j.setMsg("银行卡添加成功");
			j.setSuccess(true);
		}else{
			j.setMsg("银行卡添加失败");
		}
		super.writeJson(j, response);
	}
	
	@RequestMapping("/accountBankCardAddSuccess")
	public ModelAndView accountBankCardAddSuccess(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/accountBankCardAddSuccess");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectManage")
	public ModelAndView toProjectManage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectManage");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectBidPub")
	public ModelAndView toProjectBidPub(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectBidPub");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectModelCheck")
	public ModelAndView toProjectModelCheck(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectModelCheck");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectCheck")
	public ModelAndView toProjectCheck(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectCheck");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectBidPubRecord")
	public ModelAndView toProjectBidPubRecord(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectBidPubRecord");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectBidCom")
	public ModelAndView toProjectBidCom(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectBidCom");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectCheck2")
	public ModelAndView toProjectCheck2(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectCheck2");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectApproval")
	public ModelAndView toProjectApproval(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectApproval");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toProjectBidComRecord")
	public ModelAndView toProjectBidComRecord(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/projectBidComRecord");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toInformationManage")
	public ModelAndView toInformationManage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/informationManage");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toInformationBasic")
	public ModelAndView toInformationBasic(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/index.jsp");
		User u = (User) request.getSession().getAttribute("user");
		if(u.getUserKindCode() == 10301){
			if(u.getUserTypeCode() == 10101){//个人需求方
				mav = new ModelAndView("/WEB-INF/pages/individual/informationBasicPer");
				List<Location> locations = locationService.getLocations();
				List<PubCode> certs = pubCodeService.getCertTypes();
				log.debug("locations lenth:" + locations.size() + " certs:" + certs.size());
				mav.addObject("certs", certs);
				mav.addObject("locations", locations);
				mav.addObject("user", u);
			}else if(u.getUserTypeCode() == 10102){//企业需求方
				mav = new ModelAndView("/WEB-INF/pages/individual/informationBasicCom");
				List<Location> locations = locationService.getLocations();
				List<PubCode> certs = pubCodeService.getCertTypes();
				List<Target> targets = targetService.getTargets();
				//使用targets的recordstatus字段来临时记录用户是否曾经选择过这个选项。
				OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
				orgBusinessTargetRela.setUserId(u.getUserId());
				
				List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
				if(bTargets != null){
					log.debug("bTarget.size:" + bTargets.size());
					HashMap<Long, String> map = new HashMap<Long, String>();
					for(int i = 0; i < bTargets.size(); i++){
						OrgBusinessTargetRela o = bTargets.get(i);
						map.put(o.getOrgBusinessTargetCode(), null);
					}
					log.debug("map:" + map.toString());
					for(int j = 0; j < targets.size(); j++){
						Target t = targets.get(j);
						if(map.containsKey(t.getTargetCode())){
							t.setRecordStatusCode(2);
						}
					}
				}
				log.debug("locations lenth:" + locations.size() + " certs:" + certs.size());
				mav.addObject("certs", certs);
				mav.addObject("targets", targets);
				mav.addObject("locations", locations);
				mav.addObject("user", u);
			}
		}else if(u.getUserKindCode() == 10302){//供应方
			mav = new ModelAndView("/WEB-INF/pages/individual/informationBasicSup");
			List<Location> locations = locationService.getLocations();
			List<PubCode> certs = pubCodeService.getCertTypes();
			List<PubCode> years = pubCodeService.getOrgBusinessYearsTypes();
			List<Target> targets = targetService.getTargets();
			//使用targets的recordstatus字段来临时记录用户是否曾经选择过这个选项。
			OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
			orgBusinessTargetRela.setUserId(u.getUserId());
			
			List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
			if(bTargets != null){
				log.debug("bTarget.size:" + bTargets.size());
				HashMap<Long, String> map = new HashMap<Long, String>();
				for(int i = 0; i < bTargets.size(); i++){
					OrgBusinessTargetRela o = bTargets.get(i);
					map.put(o.getOrgBusinessTargetCode(), null);
				}
				log.debug("map:" + map.toString());
				for(int j = 0; j < targets.size(); j++){
					Target t = targets.get(j);
					if(map.containsKey(t.getTargetCode())){
						t.setRecordStatusCode(2);
					}
				}
			}
			
			log.debug("locations lenth:" + locations.size() + " certs:" + certs.size() + " targets:" + targets.size());
			mav.addObject("certs", certs);
			mav.addObject("years", years);
			mav.addObject("targets", targets);
			mav.addObject("locations", locations);
			mav.addObject("user", u);
		}
		return mav;
	}
	
	@RequestMapping("/toInformationAuthentication")
	public ModelAndView toInformationAuthentication(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/index.jsp");
		User u = (User) request.getSession().getAttribute("user");
		if(u.getUserKindCode() == 10301){
			if(u.getUserTypeCode() == 10101){//个人需求方
				mav = new ModelAndView("/WEB-INF/pages/individual/informationAuthenticationPer");
				List<Location> locations = locationService.getLocations();
				List<PubCode> certs = pubCodeService.getCertTypes();
				log.debug("locations lenth:" + locations.size() + " certs:" + certs.size());
				mav.addObject("certs", certs);
				mav.addObject("locations", locations);
				mav.addObject("user", u);
			}else if(u.getUserTypeCode() == 10102){//企业需求方
				mav = new ModelAndView("/WEB-INF/pages/individual/informationAuthenticationCom");
				List<Location> locations = locationService.getLocations();
				List<PubCode> certs = pubCodeService.getCertTypes();
				List<Target> targets = targetService.getTargets();
				//使用targets的recordstatus字段来临时记录用户是否曾经选择过这个选项。
				OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
				orgBusinessTargetRela.setUserId(u.getUserId());
				
				List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
				if(bTargets != null){
					log.debug("bTarget.size:" + bTargets.size());
					HashMap<Long, String> map = new HashMap<Long, String>();
					for(int i = 0; i < bTargets.size(); i++){
						OrgBusinessTargetRela o = bTargets.get(i);
						map.put(o.getOrgBusinessTargetCode(), null);
					}
					log.debug("map:" + map.toString());
					for(int j = 0; j < targets.size(); j++){
						Target t = targets.get(j);
						if(map.containsKey(t.getTargetCode())){
							t.setRecordStatusCode(2);
						}
					}
				}
				log.debug("locations lenth:" + locations.size() + " certs:" + certs.size());
				mav.addObject("certs", certs);
				mav.addObject("targets", targets);
				mav.addObject("locations", locations);
				mav.addObject("user", u);
			}
		}else if(u.getUserKindCode() == 10302){//供应方
			mav = new ModelAndView("/WEB-INF/pages/individual/informationAuthenticationSup");
			List<Location> locations = locationService.getLocations();
			List<PubCode> certs = pubCodeService.getCertTypes();
			List<PubCode> years = pubCodeService.getOrgBusinessYearsTypes();
			List<Target> targets = targetService.getTargets();
			//使用targets的recordstatus字段来临时记录用户是否曾经选择过这个选项。
			OrgBusinessTargetRela orgBusinessTargetRela = new OrgBusinessTargetRela();
			orgBusinessTargetRela.setUserId(u.getUserId());
			
			List<OrgBusinessTargetRela> bTargets = orgBusinessTargetRelaService.getOrgBusinessTargets(orgBusinessTargetRela);
			if(bTargets != null){
				log.debug("bTarget.size:" + bTargets.size());
				HashMap<Long, String> map = new HashMap<Long, String>();
				for(int i = 0; i < bTargets.size(); i++){
					OrgBusinessTargetRela o = bTargets.get(i);
					map.put(o.getOrgBusinessTargetCode(), null);
				}
				log.debug("map:" + map.toString());
				for(int j = 0; j < targets.size(); j++){
					Target t = targets.get(j);
					if(map.containsKey(t.getTargetCode())){
						t.setRecordStatusCode(2);
					}
				}
			}
			
			log.debug("locations lenth:" + locations.size() + " certs:" + certs.size() + " targets:" + targets.size());
			mav.addObject("certs", certs);
			mav.addObject("years", years);
			mav.addObject("targets", targets);
			mav.addObject("locations", locations);
			mav.addObject("user", u);
		}
		return mav;
	}
	
	@RequestMapping("/changeME")
	public void changeME(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Json j = new Json();
		User user = (User) request.getSession().getAttribute("user");
		String i = request.getParameter("i");
		String userMobile2 = request.getParameter("userMobile2");
		String userMobile3 = request.getParameter("userMobile3");
		String userEmail2 = request.getParameter("userEmail2");
		String userEmail3 = request.getParameter("userEmail3");
		if(i.equals("1")){
			if((Boolean) request.getSession().getAttribute("isVerifyMobileCode")){
				if(user.getUserMobile().equals(userMobile2)){
					user.setUserMobile(userMobile3);
				}else{
					j.setMsg("旧手机号码错误！");
				}
			}else{
				j.setMsg("手机验证码错误！");
			}
		}else if(i.equals("2")){
			if((Boolean) request.getSession().getAttribute("isVerifyMobileCode")){	
					user.setUserMobile(userMobile3);
			}else{
				j.setMsg("手机验证码错误！");
			}
		}else if(i.equals("3")){
				if(user.getUserEmail().equals(userEmail2)){
					user.setUserEmail(userEmail3);
				}else{
					j.setMsg("旧邮箱地址错误！");
				}
		}else if(i.equals("4")){
			user.setUserEmail(userEmail3);
		}else{
			
		}
		
		userService.updateUser(user);
		j.setSuccess(true);
		j.setMsg("更改成功");
		super.writeJson(j, response);
	}
	
	@RequestMapping("/toModelManage")
	public ModelAndView toModelManage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/modelManage");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toModelUpload")
	public ModelAndView toModelUpload(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/modelUpload");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toModelDownload")
	public ModelAndView toModelDownload(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/individual/modelDownload");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	@RequestMapping("/toPointManage")
	public ModelAndView toPointManage(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/pointManage");
		User u = (User) request.getSession().getAttribute("user");
		mav.addObject("user",u);
		return mav;
	}
	
	
	
	@Autowired
	private UserPointService userPointService;
	
	@RequestMapping("/pointConvertCash")
	public void pointConvertCash(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
			Json j = new Json();
			int point = Integer.valueOf(request.getParameter("point"));
			User user = (User) request.getSession().getAttribute("user");
			
			log.debug("convert point:" + point);
			boolean ret = userPointService.pointConvertCash(user, point);
			
			j.setSuccess(ret);
			super.writeJson(j, response);

	}
	
	@RequestMapping("/toPointToCash")
	public ModelAndView toPointToCash(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/pointToCash");
		return mav;
	}
	
	@RequestMapping("/logOut")
	public ModelAndView logOut(HttpServletRequest request) {
		User u = (User) request.getSession().getAttribute("user");
		//记录日志
		sysLogService.newLog(9804, 9701, 9901, u.getUserId());
		request.getSession().setAttribute("user", null);
		ModelAndView mav = new ModelAndView("/WEB-INF/pages/main");
		return mav;
	}
	
}
